package com.yz.jdd.module.main;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.trello.rxlifecycle.ActivityEvent;
import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseActivity;
import com.yz.jdd.base.activity.BaseFragment;
import com.yz.jdd.base.event.Events;
import com.yz.jdd.base.event.RxBus;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.base.utils.StringUtil;
import com.yz.jdd.module.main.adapter.MsgCountAdapter;
import com.yz.jdd.webview.Router;
import com.yz.jdd.webview.entity.MsgCountBean;

import java.util.List;

import rx.functions.Action1;

/**
 * 消息fragment
 * 2019.04.16
 * ljm
 */
public class MessageActivity extends BaseActivity implements View.OnClickListener {

    ListView listView;
    MsgCountAdapter adapter;

    ImageView back;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        View view = inflater.inflate(R.layout.fragment_message, container, false);
        setContentView(R.layout.activity_message);
        listView = findViewById(R.id.listView);
        back=findViewById(R.id.top_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        String jsonStr = PreferencesUtils.getString(this, BaseConfigs.PREF_MSG_COUNT, "");
        if (!StringUtil.isEmpty(jsonStr)) {
            MsgCountBean msgCountBean = new Gson().fromJson(jsonStr, MsgCountBean.class);
            initView(msgCountBean.getAppMsgVoList());
        }
        // 注册待办消息返回数据监听
        RxBus.with(this).setEvent(Events.EVENT_BACKLOG_RESPONSE).setEndEvent(ActivityEvent.DESTROY).onNext(new Action1<Events<?>>() {
            @Override
            public void call(Events<?> events) {
                String jsonStr = events.getContent();
                if (!StringUtil.isEmpty(jsonStr)) {
                    MsgCountBean msgCountBean = new Gson().fromJson(jsonStr, MsgCountBean.class);
                    initView(msgCountBean.getAppMsgVoList());
                }
            }
        }).create();

    }



    public void initView(final List<MsgCountBean.AppMsgVoListBean> appMsgVoList) {
        adapter = new MsgCountAdapter(this, appMsgVoList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Router.startWebView(MessageActivity.this, appMsgVoList.get(i).getNextStip());
            }
        });
    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        if (isVisible()) {
//            // 可见触发刷新
//            LogUtils.i("ljm  onResume 可见触发刷新");
//            RxBus.getInstance().send(Events.EVENT_BACKLOG_CHANGE, "");
//        }
//    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if (!hidden) {
//            LogUtils.i("ljm onHiddenChanged 为隐藏触发刷新");
//            RxBus.getInstance().send(Events.EVENT_BACKLOG_CHANGE, "");
//        }
//    }

    @Override
    public void onClick(View v) {

    }
}
